<script setup>



import { apiGetUserInfo, getloginsms, userget_user_info } from "@/utils/api";
import { reactive, ref, onBeforeMount, onUpdated } from "vue";

import { useRouter } from "vue-router";
import { ElMessage } from 'element-plus'

import { useStore } from "vuex";
const router = useRouter();
const store = useStore()
console.log(store.state.showlogin, '.state.showLogin') // hello


const userInfo = ref('')
const form = reactive({
  phone: '',
  phone_code: '',
})
const data = reactive({
  num: 61,
})

const gotoRouter = (val) => {
  router.push({ path: val })
};

const login = () => {
  store.commit("updateInfo", true);
}
const loginSure = () => {
  if (!form.phone || !form.phone_code) {
    ElMessage.warning('请正确输入手机号和验证码')
    return
  }
  apiGetUserInfo(form).then(res => {
    if(res.code == 200){
      localStorage.setItem('token',res.data.token)
      ElMessage.success('登录成功！')
      form.phone = ''
      form.phone_code = ''

      store.commit("updateInfo", false);
      userget_user_info().then(res => {
        userInfo.value = res.data
        localStorage.setItem('userInfo', JSON.stringify(res.data))
        location.reload()
      })
    }else{
      ElMessage.warning(res.message)
    }
  })
}
const go = () => {

  localStorage.removeItem('leftactive')
  // localStorage.setItem('leftactive', null);
  router.push({ path: '/order/orderHome' })
}
const getloginsmsFn = () => {

 
  if (!form.phone) { 
    ElMessage.warning('请先输入手机号')
    return
  }
  if (data.num == 61) {
    getloginsms({ phone: form.phone }).then(res => {
      if (res.code == 200) {
        ElMessage.success('发送成功！')
        data.num--;
        const timer = setInterval(() => {
          data.num--
          if (data.num == '-1') {
            data.num = '61'
            clearInterval(timer)
          }
        }, 1000);
      }else{ 
        ElMessage.warning(res.message)
      }
    })
  }
}
onBeforeMount(() => {
  userInfo.value = localStorage.getItem('userInfo') ? JSON.parse(localStorage.getItem('userInfo')) : null;
})
// 退出登录
const confirmLogin = () => {
  localStorage.removeItem('token')
  localStorage.removeItem('userInfo')
  userInfo.value='';
  router.push({ path: '/' })
}


</script>
<template>
  <div class="NavbarBox">
    <div class="loginBox">
      <div class="headImgbox ">
        <div class="flex" v-if="userInfo">
          <img :src="userInfo.avatar" alt="" />
          <span>{{ userInfo.nickname }}</span>
        </div>
        <div class="textBox" v-else>
          <div class="headImg"></div>
          <span class="text cur" @click="login">登录</span>
          <!-- <el-divider direction="vertical" />
        <span class="text cur">注册</span> -->
        </div>
      </div>

    </div>
   
    <div class="routerBox">
      <span class="cur" @click="gotoRouter('/')">商城首页</span>
      <span class="cur" @click="go">个人中心</span>
      <span class="cur" @click="gotoRouter('/cart')">购物车</span>
      <span class="cur" @click="gotoRouter('/collect')">收藏夹</span>

    </div>
  </div>


  <el-dialog class="diago" width="650px" v-model="store.state.showlogin">
    <template #header="{ titleId, titleClass }">
      <div class="my-header">
        <div class="title" :id="titleId" :class="titleClass">欢迎登录奈喜</div>
        <div class="tips">未注册用户将自动创建账号</div>
      </div>
    </template>

    <div class="inputPhone">
      <span>+86</span>
      <el-divider style="height:36px;margin: 0;margin-right: 20px;" direction="vertical" />
      <input v-model="form.phone" type="text" placeholder="请输入手机号" />
    </div>

    <div class="inputPhoneCode">
      <input v-model="form.phone_code" type="text" placeholder="请输入验证码" style="outline: none;" />
      <span @click="getloginsmsFn" class="cur">{{ data.num == 61 ? '发送验证码' : data.num + 's' }}</span>

    </div>

    <div class="btn cur" @click="loginSure">登录</div>

    <!-- <div class="qita">其他登录方式</div>

    <div class="wechatLogin"><img src="../assets/Image/5494c0fa4c8d21450ef7357d0929a5d8.jpeg" alt=""> <img
        src="../assets/Image/5494c0fa4c8d21450ef7357d0929a5d8.jpeg" alt=""></div> -->

    <!-- <input class="inputPhoneCode" v-model="form.phone_code" type="text" placeholder="请输入验证码" /> -->


  </el-dialog>
</template>

<style scoped lang="less">
.text:hover {
  color: #DD2131
}

.NavbarBox {
  max-width: 1920px;
  margin: 0 auto;
  background: #f5f5f5;
  height: 54px;
  line-height: 54px;
  display: flex;
  justify-content: space-between;
  padding: 0 224px;
  box-sizing: border-box;
  align-items: center;

  .routerBox {
    span {
      margin-right: 40px;
      font-size: 16px;
      font-family: PingFang SC-Regular, PingFang SC;
      font-weight: 400;
      color: #666666;
    }

    span:hover {
      color: #DD2131
    }
  }

  .loginBox {
    display: flex;
    align-items: center;

    .textBox {
      display: flex;
      align-items: center;
      font-size: 16px;
      font-family: PingFang SC-Regular, PingFang SC;
      font-weight: 400;
      color: #666666;
      line-height: 54px;

      .line {
        border-right: 1px #707070 solid;
        height: 16px !important;
        box-sizing: border-box;
        margin: 0 4px;
      }
    }

    .headImgbox {
      img {
        width: 40px;
        height: 40px;
        border-radius: 50%;
        margin-right: 10px;
      }

      .headImg {
        width: 40px;
        height: 40px;
        background: #ebebeb;
        border-radius: 50%;
        margin-right: 12px;


      }
    }

  }
}

.diago {
  /deep/.el-dialog__body {
    padding: 0 !important;
  }
}

.my-header {
  padding-top: 20px;
  margin-bottom: 13px;
  box-sizing: border-box;

  .title {
    font-size: 32px;
    font-family: PingFang SC-Semibold, PingFang SC;
    font-weight: 600;
    color: #333333;
    text-align: center;
    margin-bottom: 20px;
  }

  .tips {
    font-size: 16px;
    font-family: PingFang SC-Regular, PingFang SC;
    font-weight: 400;
    color: #999999;
    text-align: center;
  }
}

.inputPhone {
  width: calc(100% - 120px);
  height: 64px;
  margin: 0 auto;
  margin-bottom: 18px;
  box-sizing: border-box;
  border-bottom: 1px solid #CCCCCC;
  display: flex;
  align-items: center;

  input {
    border: none;
    font-size: 20px;
    font-family: PingFang SC-Regular, PingFang SC;
    font-weight: 400;
    flex: 1;
    padding-right: 10px;
    box-sizing: border-box;
  }

  span {
    width: 94px;
    display: inline-block;
    line-height: 64px;
    text-align: center;
    font-size: 20px;
    font-family: PingFang SC-Medium, PingFang SC;
    font-weight: 500;
    color: #333333;
  }

}

.inputPhoneCode {
  width: calc(100% - 120px);
  height: 64px;
  margin: 0 auto;
  margin-bottom: 18px;
  box-sizing: border-box;
  border-bottom: 1px solid #CCCCCC;
  display: flex;
  justify-content: space-between;

  input {
    border: none;
    font-size: 20px;
    font-family: PingFang SC-Regular, PingFang SC;
    font-weight: 400;
    flex: 1;
    padding-right: 10px;
    box-sizing: border-box;
  }

  span {
    display: inline-block;
    line-height: 64px;
    text-align: center;
    font-size: 20px;
    font-family: PingFang SC-Medium, PingFang SC;
    font-weight: 500;
    color: #DD2131;
    width: 100px;
  }

}

.btn {
  width: 490px;
  height: 60px;
  background: #DD2131;
  border-radius: 0px 0px 0px 0px;
  opacity: 1;
  border: 1px solid #CCCCCC;
  line-height: 60px;
  text-align: center;
  font-size: 24px;
  font-family: PingFang SC-Regular, PingFang SC;
  font-weight: 400;
  color: #FFFFFF;
  margin: 0 auto;
  margin-top: 60px;
  margin: 60px;
}

.qita {
  font-size: 16px;
  font-family: PingFang SC-Regular, PingFang SC;
  font-weight: 400;
  color: #999999;
  text-align: center;
  margin-bottom: 34px;
}

.wechatLogin {
  display: flex;
  width: 200px;
  margin: 0 auto;

  img {
    width: 48px;
    height: 48px;
    border-radius: 50%;
    margin: 0 40px;
  }

  img:nth-child(1) {
    margin-left: 0;
  }

  img:nth-child(2) {
    margin-right: 0;
  }

}
</style>
